/*
 * @Desc:
 * @author cheegon
 * @date 2024/7/24  上午9:24
**/
import Vue from 'vue'
import VueRouter from 'vue-router'
import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css'// Progress 进度条样式
import { getToken } from '../config/authConfig'
import globalConfig from '../config/globalConfig.js'



// 注册路由插件
Vue.use(VueRouter)

// 遍历导入route
let routes = []
const routerContext = import.meta.glob('./*.js', { eager: true });
for (const path in routerContext) {
    const routerModule = routerContext[path].default
    routes = [...routes, ...(routerModule.default || routerModule)]
}

const router = new VueRouter({
   // base: process.env.NODE_ENV === 'production' ? `/${ globalConfig.appName }/` : '/', // 如果是多项目的话,需要这个
    scrollBehavior: () => ({ y: 0 }),
    routes
})

router.beforeEach((to, from, next) => {
    NProgress.start()
    if (to.meta && to.meta.noLogin) {
        next()
    } else {
        if (getToken()) {
            next()
        } else {
            next({
                path: '/login',
                replace: true
            })
        }
    }
})
router.afterEach(() => NProgress.done())

export default router
